home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / PASCAL / MISC_ROU / PTRARITH.P < prev    next >
Text File  |  1990-10-23  |  9KB  |  350 lines

  1. G'day all Pascal programmers who handle pointer arithmetic.  I recently did a 
  2. job in which I became heartily sick of writing:
  3. aPtr:=ptr(ord4(aPtr)+1);
  4.  
  5. so - here's a unit full of increment and decrement functions and procedures, 
  6. for Integer, Ptr and Longint type variables.  Just stick in your project and
  7. enter ptrTools in your 'uses' clause.
  8.  
  9. NOTE:  there are basically 8 functions and procedures for each of the three
  10. data types.  The procedures are called inc, inc2, inc, inc4 or dec, dec1 etc...
  11. The functions are defined as you often want to peek at the next value without
  12. changing your current pointer.  Hence Plus1, Plus2, ... and Less1 ...
  13.  
  14. You will see that all routines are typed by a P, L or I character at the end
  15. of the name.
  16.  
  17. Finally there's 'charAt' which should be used if you are using pointer 
  18. arithmetic to scan through memory.  Odd addresses can cause funny problems (
  19. although a lot of Toolbox calls seem to handle them ok).
  20.  
  21. Share and Enjoy
  22.  
  23. Andy
  24.  
  25. PS:  for the less-knowledgeable, the "inline" stuff means that the two words of 
  26. machine code defined will be copied inline into the calling program, rather 
  27. than compiling in a call to a procedure.  Thus, these routines are the fastest
  28. way to increment pointers etc. around!!
  29.  
  30.  
  31. unit ptrTools;
  32. { functions for incrementing and decrementing various 2 and 4 byte values }
  33. {  EG:  incP(aPtr)      nextPtr:=plus1P(aPtr)        less4I(integerToDecrease) }
  34.  
  35. { copyright 1990 A.D. Software, free for all use provided this notice retained }
  36. {Andy Dent                                    A.D. Software phone 09 249 2719 }
  37. {    Mac & VAX programmer           94 Bermuda Dve , Ballajura}
  38. {    a_dent@ ennel.cc.uwa.oz           Western Australia 6066}
  39. {    a_dent@fennel.cc.uwa.oz AU ( international ) }
  40.  
  41. { NOTE:  if you want to increase the constants (up to 8) then change the }
  42. { second digit of the SUBQ or ADDQ line }
  43. {  ADD is even, 0=8  2=1  4=2  6=3  8=4  A=5  C=6  E=7 }
  44. {  SUB is odd,   1=8  3=1  5=2  7=3  9=4  B=5  D=6  F=7 }
  45.  
  46. interface
  47.  
  48.     function charAt (p: ptr): char;
  49. { returns character at pointer - needed because an Odd pointer will cause a }
  50. { bus error if we try to get just the value at that point.  NOTE:  you don't }
  51. { need to worry about passing odd pointers to the Toolbox routines as they }
  52. { also are smart enough to cope. }
  53.  
  54. {******************************************************************************}
  55. {                                   L O N G I N T                             }
  56. {******************************************************************************}
  57.  
  58. {                                 F U N C T I O N S                        }
  59.  
  60.     function Plus1L (l: longint): longint;
  61.     inline
  62.         $5297, {ADDQ.L #$1, (A7) }
  63.         $2E9F;  {MOVE.L (A7)+, (A7) }
  64.  
  65.     function Plus2L (l: longint): longint;
  66.     inline
  67.         $5497, {ADDQ.L #$2, (A7) }
  68.         $2E9F;  {MOVE.L (A7)+, (A7) }
  69.  
  70.     function Plus3L (l: longint): longint;
  71.     inline
  72.         $5697, {ADDQ.L #$3, (A7) }
  73.         $2E9F;  {MOVE.L (A7)+, (A7) }
  74.  
  75.     function Plus4L (l: longint): longint;
  76.     inline
  77.         $5897, {ADDQ.L #$4, (A7) }
  78.         $2E9F;  {MOVE.L (A7)+, (A7) }
  79.  
  80.  
  81.     function Less1L (l: longint): longint;
  82.     inline
  83.         $5397, {SUBQ.L #$1, (A7) }
  84.         $2E9F;  {MOVE.L (A7)+, (A7) }
  85.  
  86.     function Less2L (l: longint): longint;
  87.     inline
  88.         $5597, {SUBQ.L #$2, (A7) }
  89.         $2E9F;  {MOVE.L (A7)+, (A7) }
  90.  
  91.     function Less3L (l: longint): longint;
  92.     inline
  93.         $5797, {SUBQ.L #$3, (A7) }
  94.         $2E9F;  {MOVE.L (A7)+, (A7) }
  95.  
  96.     function Less4L (l: longint): longint;
  97.     inline
  98.         $5997, {SUBQ.L #$4, (A7) }
  99.         $2E9F;  {MOVE.L (A7)+, (A7) }
  100.  
  101.  
  102.  
  103.  
  104. {                     P R O C E D U R E S                           }
  105.     procedure incL (var l: longint);
  106.     inline
  107.         $205f, {MOVEA.L (A7)+, A0 }
  108.         $5290; {ADDQ.L #$1, (A0) }
  109.  
  110.     procedure inc2L (var l: longint);
  111.     inline
  112.         $205f, {MOVEA.L (A7)+, A0 }
  113.         $5490; {ADDQ.L #$2, (A0) }
  114.  
  115.     procedure inc3L (var l: longint);
  116.     inline
  117.         $205f, {MOVEA.L (A7)+, A0 }
  118.         $5690; {ADDQ.L #$3, (A0) }
  119.  
  120.     procedure inc4L (var l: longint);
  121.     inline
  122.         $205f, {MOVEA.L (A7)+, A0 }
  123.         $5890; {ADDQ.L #$4, (A0) }
  124.  
  125.  
  126.     procedure decL (var l: longint);
  127.     inline
  128.         $205f, {MOVEA.L (A7)+, A0 }
  129.         $5390; {SUBQ.L #$1, (A0) }
  130.  
  131.     procedure dec2L (var l: longint);
  132.     inline
  133.         $205f, {MOVEA.L (A7)+, A0 }
  134.         $5590; {SUBQ.L #$2, (A0) }
  135.  
  136.     procedure dec3L (var l: longint);
  137.     inline
  138.         $205f, {MOVEA.L (A7)+, A0 }
  139.         $5790; {SUBQ.L #$3, (A0) }
  140.  
  141.     procedure dec4L (var l: longint);
  142.     inline
  143.         $205f, {MOVEA.L (A7)+, A0 }
  144.         $5990; {SUBQ.L #$4, (A0) }
  145.  
  146.  
  147.  
  148. {******************************************************************************}
  149. {                                     P T R                                    }
  150. {******************************************************************************}
  151.  
  152. {                                 F U N C T I O N S                        }
  153.     function Plus1P (p: ptr): ptr;
  154.     inline
  155.         $5297, {ADDQ.L #$1, (A7) }
  156.         $2E9F;  {MOVE.P (A7)+, (A7) }
  157.  
  158.     function Plus2P (p: ptr): ptr;
  159.     inline
  160.         $5497, {ADDQ.L #$2, (A7) }
  161.         $2E9F;  {MOVE.P (A7)+, (A7) }
  162.  
  163.     function Plus3P (p: ptr): ptr;
  164.     inline
  165.         $5697, {ADDQ.L #$3, (A7) }
  166.         $2E9F;  {MOVE.P (A7)+, (A7) }
  167.  
  168.     function Plus4P (p: ptr): ptr;
  169.     inline
  170.         $5897, {ADDQ.L #$4, (A7) }
  171.         $2E9F;  {MOVE.P (A7)+, (A7) }
  172.  
  173.  
  174.     function Less1P (p: ptr): ptr;
  175.     inline
  176.         $5397, {SUBQ.L #$1, (A7) }
  177.         $2E9F;  {MOVE.P (A7)+, (A7) }
  178.  
  179.     function Less2P (p: ptr): ptr;
  180.     inline
  181.         $5597, {SUBQ.L #$2, (A7) }
  182.         $2E9F;  {MOVE.P (A7)+, (A7) }
  183.  
  184.     function Less3P (p: ptr): ptr;
  185.     inline
  186.         $5797, {SUBQ.L #$3, (A7) }
  187.         $2E9F;  {MOVE.P (A7)+, (A7) }
  188.  
  189.     function Less4P (p: ptr): ptr;
  190.     inline
  191.         $5997, {SUBQ.L #$4, (A7) }
  192.         $2E9F;  {MOVE.P (A7)+, (A7) }
  193.  
  194.  
  195.  
  196.  
  197. {                     P R O C E D U R E S                           }
  198.     procedure incP (var p: ptr);
  199.     inline
  200.         $205f, {MOVEA.P (A7)+, A0 }
  201.         $5290; {ADDQ.L #$1, (A0) }
  202.  
  203.     procedure inc2P (var p: ptr);
  204.     inline
  205.         $205f, {MOVEA.P (A7)+, A0 }
  206.         $5490; {ADDQ.L #$2, (A0) }
  207.  
  208.     procedure inc3P (var p: ptr);
  209.     inline
  210.         $205f, {MOVEA.P (A7)+, A0 }
  211.         $5690; {ADDQ.L #$3, (A0) }
  212.  
  213.     procedure inc4P (var p: ptr);
  214.     inline
  215.         $205f, {MOVEA.P (A7)+, A0 }
  216.         $5890; {ADDQ.L #$4, (A0) }
  217.  
  218.  
  219.     procedure decP (var p: ptr);
  220.     inline
  221.         $205f, {MOVEA.P (A7)+, A0 }
  222.         $5390; {SUBQ.L #$1, (A0) }
  223.  
  224.     procedure dec2P (var p: ptr);
  225.     inline
  226.         $205f, {MOVEA.P (A7)+, A0 }
  227.         $5590; {SUBQ.L #$2, (A0) }
  228.  
  229.     procedure dec3P (var p: ptr);
  230.     inline
  231.         $205f, {MOVEA.P (A7)+, A0 }
  232.         $5790; {SUBQ.L #$3, (A0) }
  233.  
  234.     procedure dec4P (var p: ptr);
  235.     inline
  236.         $205f, {MOVEA.P (A7)+, A0 }
  237.         $5990; {SUBQ.L #$4, (A0) }
  238.  
  239.  
  240.  
  241.  
  242. {******************************************************************************}
  243. {                                    I N T E G E R                             }
  244. {******************************************************************************}
  245.  
  246. {                                 F U N C T I O N S                        }
  247.     function Plus1I (i: integer): integer;
  248.     inline
  249.         $5257, {ADDQ.W #$1, (A7) }
  250.         $3E9F;  {MOVE.W (A7)+, (A7) }
  251.  
  252.     function Plus2I (i: integer): integer;
  253.     inline
  254.         $5457, {ADDQ.W #$2, (A7) }
  255.         $3E9F;  {MOVE.W (A7)+, (A7) }
  256.  
  257.     function Plus3I (i: integer): integer;
  258.     inline
  259.         $5657, {ADDQ.W #$3, (A7) }
  260.         $3E9F;  {MOVE.W (A7)+, (A7) }
  261.  
  262.     function Plus4I (i: integer): integer;
  263.     inline
  264.         $5857, {ADDQ.W #$4, (A7) }
  265.         $3E9F;  {MOVE.W (A7)+, (A7) }
  266.  
  267.  
  268.     function Less1I (i: integer): integer;
  269.     inline
  270.         $5357, {SUBQ.W #$1, (A7) }
  271.         $3E9F;  {MOVE.W (A7)+, (A7) }
  272.  
  273.     function Less2I (i: integer): integer;
  274.     inline
  275.         $5557, {SUBQ.W #$2, (A7) }
  276.         $3E9F;  {MOVE.W (A7)+, (A7) }
  277.  
  278.     function Less3I (i: integer): integer;
  279.     inline
  280.         $5757, {SUBQ.W #$3, (A7) }
  281.         $3E9F;  {MOVE.W (A7)+, (A7) }
  282.  
  283.     function Less4I (i: integer): integer;
  284.     inline
  285.         $5957, {SUBQ.W #$4, (A7) }
  286.         $3E9F;  {MOVE.W (A7)+, (A7) }
  287.  
  288.  
  289.  
  290.  
  291. {                     P R O C E D U R E S                           }
  292.     procedure incI (var i: integer);
  293.     inline
  294.         $205f, {MOVEA.W (A7)+, A0 }
  295.         $5250; {ADDQ.W #$1, (A0) }
  296.  
  297.     procedure inc2I (var i: integer);
  298.     inline
  299.         $205f, {MOVEA.W (A7)+, A0 }
  300.         $5450; {ADDQ.W #$2, (A0) }
  301.  
  302.     procedure inc3I (var i: integer);
  303.     inline
  304.         $205f, {MOVEA.W (A7)+, A0 }
  305.         $5650; {ADDQ.W #$3, (A0) }
  306.  
  307.     procedure inc4I (var i: integer);
  308.     inline
  309.         $205f, {MOVEA.W (A7)+, A0 }
  310.         $5850; {ADDQ.W #$4, (A0) }
  311.  
  312.  
  313.     procedure decI (var i: integer);
  314.     inline
  315.         $205f, {MOVEA.W (A7)+, A0 }
  316.         $5350; {SUBQ.W #$1, (A0) }
  317.  
  318.     procedure dec2I (var i: integer);
  319.     inline
  320.         $205f, {MOVEA.W (A7)+, A0 }
  321.         $5550; {SUBQ.W #$2, (A0) }
  322.  
  323.     procedure dec3I (var i: integer);
  324.     inline
  325.         $205f, {MOVEA.W (A7)+, A0 }
  326.         $5750; {SUBQ.W #$3, (A0) }
  327.  
  328.     procedure dec4I (var i: integer);
  329.     inline
  330.         $205f, {MOVEA.W (A7)+, A0 }
  331.         $5950; {SUBQ.W #$4, (A0) }
  332.  
  333. implementation
  334.  
  335.     function charAt (p: ptr): char;
  336.         type
  337.             charsInAWord = packed array[1..2] of char;
  338.             charsInAWordPtr = ^charsInAWord;
  339.         var
  340.             w: charsInAWord;
  341.     begin
  342.         if Odd(longint(p)) then begin
  343.             w := charsInAWordPtr(Less1P(p))^;  { uses Less1P }
  344.             charAt := w[2];               { defined in this unit }
  345.         end                                   {to get byte before P }
  346.         else
  347.             charAt := char(p^);
  348.     end;  { charAt }
  349. end.
  350.